package cn.itsource.pethome.base.controller;

//我们要去抽什么东西出来
//公共的  添加 修改 查询1一个 多个 分页  删除

import cn.itsource.pethome.base.query.Page;
import cn.itsource.pethome.base.service.IBaseService;
import cn.itsource.pethome.util.AjaxResult;
import cn.itsource.pethome.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public class BaseController<T> {
    @Autowired
    private IBaseService<T> baseService;

    @PostMapping("/pagelist")
    public PageList<T> pageQueryAll(@RequestBody Page page){
        return baseService.pageQueryAll(page);
    }


    @GetMapping("/list")
    public List<T> queryAll(){
        return baseService.queryAll();
    }

    @GetMapping("/{id}")
    public T queryOne(@PathVariable("id") Long id){
       return baseService.queryOne(id);
    }

    //添加
    @PostMapping
    public AjaxResult insert(@RequestBody T t){
        try {
            baseService.insert(t);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    //添加
    @PutMapping
    public AjaxResult update(@RequestBody T t){
        try {
            baseService.update(t);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    //删除
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            baseService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

}
